<?php

namespace XWAM\Controller;

/**
 * 会员控制器
 *
 * @since 1.0 <2016-8-22> sutroon Added.
 */
class MemberController extends AppbaseController {

    function user_list_search() {
        $search = $_GET;
        if (!empty($search['search_key'])) {
            $searchkey = $search['search_key'];
            $where['_string'] = "user_name like '%$searchkey%' or name like '%$searchkey%' or mobile like '%$searchkey%'";
        }
        if (!empty($search['user_name'])) {
            $where['user_name'] = array('like', '%' . $search['user_name'] . '%');
        }
        $search['type_name'] = 'MEMBER';
        $where['type_name'] = $search['type_name'];
        $where['site_id'] = $this->site_id;
        return array('search' => $search, 'where' => $where);
    }

    public function user_list() {
        $asearch = $this->user_list_search();
        $m_member = new \XWAM\Model\MemberModel();
        $list = $m_member->get_paging_list($page, array('where' => $asearch['where'], 'order' => 'user_name'), array('page_params' => $asearch['search']));
        $this->assign('search', $asearch['search']);
        $this->assign('list', $list);
        $this->assign('page', $page->show());
        $this->display_cpp();
    }

    function get_user_item_json($id = '', $username = '') {
        $where = array();
        if ($id) {
            $where = array('id' => $id);
        }
        if ($username) {
            $where = array('user_name' => $username);
        }
        if (!$where) {
            $this->ajaxReturn(null);
        }
        $m_member = new \XWAM\Model\MemberModel();
        $this->ajaxReturn($m_member->where($where)->find());
    }

    public function user_export() {
        $m_user = new \XWAM\Model\UserModel();
        $list = $m_user->select();
        if (!$list) {
            $this->error('找不到符合条件的记录!');
        }
        sofn_excel_export($list);
    }

    /**
     * 编辑个人资料
     * @since 1.0 <2015-11-11> SoChishun Added.
     */
    function profile_edit() {
        $m_member = new \XWAM\Model\MemberModel();
        $data = $m_member->find_seat($this->user_login_data['id']);
        $this->assign('data', $data);
        $this->display();
    }

    public function profile_edit_save() {
        $m_member = new \XWAM\Model\MemberModel();
        $result = $m_member->save_user($this->user_login_data['id']);
        $this->dialogClose($result['status'], array('success' => '保存成功!<br /><span style="font-size:12px">如有变更[来电弹屏模式]内容，需刷新浏览器才会生效!</span>', 'error' => $result['info'], 'success_action' => '!window.parent.location.reload();', 'ajax' => 5));
    }

    public function user_edit($id = 0) {
        $data = array();
        if ($id) {
            $m_member = new \XWAM\Model\MemberModel();
            $data = $m_member->find($id);
        }
        if (!$data) {
            $data['site_id'] = $this->site_id;
        }
        $this->assign('data', $data);
        $this->display();
    }

    public function user_edit_save() {
        $m_member = new \XWAM\Model\MemberModel();
        $result = $m_member->save_user($this->user_login_data['id']);
        $this->dialogJump($result['status'], $result['info']);
    }

    public function user_delete($id = '') {
        if (!$id) {
            $this->ajaxMsg(false, '编号丢失!');
        }
        $m_member = new \XWAM\Model\MemberModel();
        $result = $m_member->delete_user($id, $this->user_login_data['id']);
        $this->ajaxMsg($result, '删除{%}!');
    }

    public function user_password_reset($id) {
        if (!$id) {
            $this->ajaxMsg(false, '参数有误!');
        }
        $m_member = new \XWAM\Model\MemberModel();
        $result = $m_member->where(array('id' => $id))->setField('password', '123456');
        $this->ajaxMsg($result, '密码重置{%}!');
    }

    function change_online_status($id, $status) {
        $m_user = new \XWAM\Model\UserModel();
        $this->ajaxReturn($m_user->change_online_status($id, $status));
    }

}
